<!-- 预定成功页面 -->

<!DOCTYPE html>
<html>

<?php
// 引入session_customer.php文件
include('session_customer.php');
// 检查是否设置了$_SESSION['login_customer']变量
if (!isset($_SESSION['login_customer'])) {
    // 销毁session
    session_destroy();
    // 重定向到customerlogin.php页面
    header("location: customerlogin.php");
}
?>

<head>

    <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
    <link rel="shortcut icon" type="image/png" href="assets/img/P.png.png">
    <link rel="stylesheet" href="assets/w3css/w3.css">
    <script type="text/javascript" src="assets/js/jquery.min.js"></script>
    <script type="text/javascript" src="assets/js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" media="screen" href="assets/css/bookingconfirm.css" />
</head>

<body>

    <?php
    // 获取表单提交的数据
    $type = $_POST['radio']; // 车辆类型
    $charge_type = $_POST['radio1']; // 计费方式
    $driver_id = $_POST['driver_id_from_dropdown']; // 司机ID
    $customer_username = $_SESSION["login_customer"]; // 客户用户名
    $car_id = $conn->real_escape_string($_POST['hidden_carid']); // 车辆ID
    $rent_start_date = date('Y-m-d', strtotime($_POST['rent_start_date'])); // 租车起始日期
    $rent_end_date = date('Y-m-d', strtotime($_POST['rent_end_date'])); // 租车结束日期
    $return_status = "NR"; // 未归还状态
    $fare = "NA"; // 费用（初值为NA）

    // 计算日期差
    function dateDiff($start, $end)
    {
        $start_ts = strtotime($start);
        $end_ts = strtotime($end);
        $diff = $end_ts - $start_ts;
        return round($diff / 86400);
    }

    $err_date = dateDiff("$rent_start_date", "$rent_end_date"); // 计算起始日期到结束日期的天数差

    // 查询对应车辆ID的车辆信息
    $sql0 = "SELECT * FROM cars WHERE car_id = '$car_id'";
    $result0 = $conn->query($sql0);

    // 如果查询结果大于0，遍历结果集获取车辆信息
    if (mysqli_num_rows($result0) > 0) {
        while ($row0 = mysqli_fetch_assoc($result0)) {
            // 根据车辆类型和计费方式确定相应的费用
            if ($type == "ac" && $charge_type == "km") {
                $fare = $row0["ac_price"]; // 空调车按公里计费价格
            } else if ($type == "ac" && $charge_type == "days") {
                $fare = $row0["ac_price_per_day"]; // 空调车按天计费价格
            } else if ($type == "non_ac" && $charge_type == "km") {
                $fare = $row0["non_ac_price"]; // 非空调车按公里计费价格
            } else if ($type == "non_ac" && $charge_type == "days") {
                $fare = $row0["non_ac_price_per_day"]; // 非空调车按天计费价格
            } else {
                $fare = "NA"; // 费用为NA，表示未确定
            }
        }
    }

    if ($err_date >= 0) {
        // 插入租车记录到数据库
        $sql1 = "INSERT INTO rentedcars(customer_username, car_id, driver_id, booking_date, rent_start_date, 
        rent_end_date, fare, charge_type, return_status) 
        VALUES('" . $customer_username . "', '" . $car_id . "', '" . $driver_id . "', '" . date("Y-m-d") . "', '" .
            $rent_start_date . "', '" . $rent_end_date . "', '" . $fare . "', '" . $charge_type . "', '" . $return_status . "')";
        $result1 = $conn->query($sql1);

        // 将对应的车辆设置为不可用状态
        $sql2 = "UPDATE cars SET car_availability = 'no' WHERE car_id = '$car_id'";
        $result2 = $conn->query($sql2);

        // 将对应的司机设置为不可用状态
        $sql3 = "UPDATE driver SET driver_availability = 'no' WHERE driver_id = '$driver_id'";
        $result3 = $conn->query($sql3);

        // 查询租借信息和相关表中的记录
        $sql4 = "SELECT * FROM cars c, clients cl, driver d, rentedcars rc WHERE c.car_id = '$car_id' 
        AND d.driver_id = '$driver_id' AND cl.client_username = d.client_username";
        $result4 = $conn->query($sql4);

        // 如果查询结果大于0，提取相应数据
        if (mysqli_num_rows($result4) > 0) {
            while ($row = mysqli_fetch_assoc($result4)) {
                $id = $row["id"];
                $car_name = $row["car_name"];
                $car_nameplate = $row["car_nameplate"];
                $driver_name = $row["driver_name"];
                $driver_gender = $row["driver_gender"];
                $dl_number = $row["dl_number"];
                $driver_phone = $row["driver_phone"];
                $client_name = $row["client_name"];
                $client_phone = $row["client_phone"];
            }
        }

        // 如果任何一步的查询失败，则输出错误信息
        if (!$result1 || !$result2 || !$result3) {
            die("无法插入数据：" . $conn->error);
        }

    ?>

        <!-- 导航栏 -->
        <?php
        include './nav.php';
        ?>



        <div class="container">
            <!-- 确认预订的提示信息 -->
            <div class="jumbotron">
                <h1 class="text-center" style="color: green;"><span class="glyphicon glyphicon-ok-circle"></span> 预订确认</h1>
            </div>
        </div>
        <br>

        <h2 class="text-center">感谢您使用车辆租赁系统！祝您旅途愉快。</h2>

        <h3 class="text-center"><strong>订单号：</strong><span style="color: blue;"><?php echo "$id"; ?></span></h3>

        <div class="container">
            <h5 class="text-center">请仔细阅读关于您的订单的以下信息。</h5>
            <div class="box">
                <div class="col-md-10" style="float: none; margin: 0 auto; text-align: center;">
                    <h3 style="color: orange;">您的预订已被确认，并已放入我们的订单处理系统中。</h3>
                    <br>
                    <h4>请现在记录您的<strong>订单号</strong>，以备需要与我们沟通订单时使用。</h4>
                    <br>
                    <h3 style="color: orange;">发票</h3>
                    <br>
                </div>
                <div class="col-md-10" style="float: none; margin: 0 auto; ">
                    <h4><strong>车辆名称：</strong><?php echo $car_name; ?></h4>
                    <br>
                    <h4><strong>车辆编号：</strong><?php echo $car_nameplate; ?></h4>
                    <br>

                    <?php
                    if ($charge_type == "days") {
                    ?>
                        <h4><strong>费用：</strong>Rs. <?php echo $fare; ?>/天</h4>
                    <?php } else {
                    ?>
                        <h4><strong>费用：</strong>Rs. <?php echo $fare; ?>/公里</h4>

                    <?php } ?>

                    <br>
                    <h4><strong>预订日期：</strong><?php echo date("Y-m-d"); ?></h4>
                    <br>
                    <h4><strong>起始日期：</strong><?php echo $rent_start_date; ?></h4>
                    <br>
                    <h4><strong>归还日期：</strong><?php echo $rent_end_date; ?></h4>
                    <br>
                    <h4><strong>司机姓名：</strong><?php echo $driver_name; ?></h4>
                    <br>
                    <h4><strong>司机性别：</strong><?php echo $driver_gender; ?></h4>
                    <br>
                    <h4><strong>司机驾驶证号：</strong><?php echo $dl_number; ?></h4>
                    <br>
                    <h4><strong>司机联系方式：</strong><?php echo $driver_phone; ?></h4>
                    <br>
                    <h4><strong>雇员姓名：</strong><?php echo $client_name; ?></h4>
                    <br>
                    <h4><strong>雇员联系方式：</strong><?php echo $client_phone; ?></h4>
                    <br>
                </div>
            </div>
            <div class="col-md-12" style="float: none; margin: 0 auto; text-align: center;">
                <h6>警告！<strong>请不要重新加载此页面</strong>，否则上述显示内容将丢失。如果您想要本页面的实体副本，请立即打印。</h6>
            </div>
        </div>
</body>
<?php } else { ?>
    <!-- 导航 -->
    <nav class="navbar navbar-custom navbar-fixed-top" role="navigation" style="color: black">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-main-collapse">
                    <i class="fa fa-bars"></i>
                </button>
                <!-- 网站品牌 -->
                <a class="navbar-brand page-scroll" href="index.php">
                    租车服务 </a>
            </div>
            <!-- 收集导航链接、表单和其他内容以进行切换 -->

            <?php
            if (isset($_SESSION['login_client'])) {
            ?>
                <div class="collapse navbar-collapse navbar-right navbar-main-collapse">
                    <ul class="nav navbar-nav">
                        <li>
                            <a href="index.php">首页</a>
                        </li>
                        <li>
                            <a href="#"><span class="glyphicon glyphicon-user"></span>欢迎您，<?php echo $_SESSION['login_client']; ?></a>
                        </li>
                        <li>
                            <ul class="nav navbar-nav navbar-right">
                                <li><a href="#" class="dropdown-toggle active" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span class="glyphicon glyphicon-user"></span>控制面板 <span class="caret"></span> </a>
                                    <ul class="dropdown-menu">
                                        <li> <a href="entercar.php">添加车辆</a></li>
                                        <li> <a href="enterdriver.php"> 添加司机</a></li>
                                        <li> <a href="clientview.php">查看</a></li>

                                    </ul>
                                </li>
                            </ul>
                        </li>
                        <li>
                            <a href="logout.php"><span class="glyphicon glyphicon-log-out"></span>退出登录</a>
                        </li>
                    </ul>
                </div>

            <?php
            } else if (isset($_SESSION['login_customer'])) {
            ?>
                <div class="collapse navbar-collapse navbar-right navbar-main-collapse">
                    <ul class="nav navbar-nav">
                        <li>
                            <a href="index.php">首页</a>
                        </li>
                        <li>
                            <a href="#"><span class="glyphicon glyphicon-user"></span>欢迎您，<?php echo $_SESSION['login_customer']; ?></a>
                        </li>
                        <ul class="nav navbar-nav">
                            <li><a href="#" class="dropdown-toggle active" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">我的车库 <span class="caret"></span> </a>
                                <ul class="dropdown-menu">
                                    <li> <a href="prereturncar.php">立即归还</a></li>
                                    <li> <a href="mybookings.php">我的预订</a></li>
                                </ul>
                            </li>
                        </ul>
                        <li>
                            <a href="logout.php"><span class="glyphicon glyphicon-log-out"></span>退出登录</a>
                        </li>
                    </ul>
                </div>

            <?php
            } else {
            ?>

                <div class="collapse navbar-collapse navbar-right navbar-main-collapse">
                    <ul class="nav navbar-nav">
                        <li>
                            <a href="index.php">首页</a>
                        </li>
                        <li>
                            <a href="clientlogin.php">员工登录</a>
                        </li>
                        <li>
                            <a href="customerlogin.php">客户登录</a>
                        </li>
                        <li>
                            <a href="#">常见问题</a>
                        </li>
                    </ul>
                </div>
            <?php   }
            ?>
            <!-- /.导航链接 -->
        </div>
        <!-- /. -->
    </nav>
    <div class="container">
        <div class="jumbotron" style="text-align: center;">
            您选择了错误的日期。
            <br><br>
        </div>
    <?php } ?>
    <footer class="site-footer">
        <div class="container">
            <hr>
            <div class="row">
                <div class="col-sm-6">
                    <h5>© <?php echo date("Y"); ?> 租车服务</h5>
                </div>
            </div>
        </div>
    </footer>

</html>